package com.avic.rong.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.avic.rong.R;
import com.avic.rong.bean.mine.InterestedBean;
import com.avic.rong.questionOrAnswer.StringUtil;
import com.avic.rong.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

public class FocusAreaAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private ArrayList<InterestedBean.InterestListBean> list;

    private ArrayList<String> strings = new ArrayList<>();
    private ArrayList<String> labels = new ArrayList<>();

    public ArrayList<String> getLabels() {
        return labels;
    }

    private String userLable;

    public void setUserLable(String userLable) {
        this.userLable = userLable;
    }

    private static int TYPE_ONE = 1;
    private static int TYPE_TWO = 2;

    public FocusAreaAdapter(Context mContext, ArrayList<InterestedBean.InterestListBean> list) {
        this.mContext = mContext;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_focus,null);
        return new FocusHolder(inflate);
    }

    public void setData(List<InterestedBean.InterestListBean> beans, String userLable){
        if (beans!=null&&userLable!=null) {
            list.addAll(beans);
            this.userLable = userLable;
        }
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        InterestedBean.InterestListBean bean = list.get(i);
        FocusHolder focusHolder = (FocusHolder) viewHolder;
        focusHolder.cb_focus.setText(bean.getValue());

        if (!StringUtil.isEmpty(userLable)){
            String[] split = userLable.split(",");
            for (int j = 0; j < split.length; j++) {
                Integer integer = Integer.valueOf(split[j]);
                Log.e("tag", "onBindViewHolder: "+integer );
                if (bean.getId() == integer){
                    focusHolder.cb_focus.setChecked(true);
                    labels.add(bean.getId()+"");
                    strings.add(bean.getValue());
                }
            }
        }


        focusHolder.cb_focus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = String.valueOf(bean.getId());
                String value = bean.getValue();
                boolean checked = focusHolder.cb_focus.isChecked();
                if (checked){
                    if (!strings.contains(value)&&!labels.contains(id)) {
                        strings.add(value);
                        labels.add(id);
                    }
                }else {
                    if (strings.contains(value)&&labels.contains(id)) {
                        strings.remove(value);
                        labels.remove(id);
                    }
                }
            }
        });



    }

    public ArrayList<String> getStrings() {
        return strings;
    }

    @Override
    public int getItemViewType(int position) {
        String s = list.get(position).getValue() ;
        if (s.length()>7){
            return TYPE_ONE;
        }
        return TYPE_TWO;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class FocusHolder extends RecyclerView.ViewHolder {
        CheckBox cb_focus;
        public FocusHolder(@NonNull View itemView) {
            super(itemView);
            cb_focus = itemView.findViewById(R.id.cb_focus);
        }
    }

    private ItemOnClick itemOnClick;

    public void setItemOnClick(ItemOnClick itemOnClick) {
        this.itemOnClick = itemOnClick;
    }

    public interface ItemOnClick{
        void onClick(InterestedBean.InterestListBean bean);
    }

}
